package db.tables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.db.BeanBase;
import com.db.Table;

import csv.imp.CsvFileColData;
import csv.imp.CsvFileRowData;

import db.beans.T_ResearchResultBean;

public class T_ResearchResultTable extends Table{
	
	public static final String COL_SRC_PRODUCT_CODE = "SRC_PRODUCT_CODE";
	public static final String COL_DES_PRODUCT_CODE = "DES_PRODUCT_CODE";
	public static final String COL_DIFFERENCE = "DIFFERENCE";
	public static final String COL_STATUS = "STATUS";
	
	private final String tableName;
	
	public T_ResearchResultTable(String tableName){
		this.tableName = tableName;
	}
	
	public String getTableName(){
		return this.tableName;
	}
	
	public static BeanBase[] selectAll(Connection conn) throws SQLException{
		final String sql = "SELECT ";

		PreparedStatement stmt = null;
		ResultSet rs = null;
		Vector<T_ResearchResultBean> v = new Vector<T_ResearchResultBean>();
		try {
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			int i;
			while (rs.next()) {
				i = 0;
				T_ResearchResultBean rec = new T_ResearchResultBean();
				
				v.add(rec);
			}
		} finally {
			if (rs != null) {
				rs.close();
			}
			if (stmt != null) {
				stmt.close();
			}
		}
		return (T_ResearchResultBean[]) v.toArray(new T_ResearchResultBean[v.size()]);
	}
	
	public static void insert(Connection conn, String strTableName, T_ResearchResultBean[] arrResultData) throws SQLException{
		final String sql = "INSERT INTO " + strTableName + "(" + COL_SRC_PRODUCT_CODE 
						+ "," + COL_DES_PRODUCT_CODE 
						+ "," + COL_DIFFERENCE 
						+ "," + COL_STATUS 
						+ ") VALUES (?,?,?,?)";
		PreparedStatement stmt = null;
		try {
			stmt = conn.prepareStatement(sql);
			int index = 0;
			for (int i = 0; i < arrResultData.length; i++){
				stmt.setLong(++index, arrResultData[i].getSrcProductCode());
				stmt.setLong(++index, arrResultData[i].getDesProductCode());
				stmt.setDouble(++index, arrResultData[i].getDifference());
				stmt.setInt(++index, arrResultData[i].getStatus());
				stmt.addBatch();
			}
			
			stmt.executeBatch();
		} finally{
			if(stmt!=null) {
				stmt.close();
			}
			conn.commit();
		}
	}
}

